<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class ChinaArea extends Model
{
    protected $table = 'china_area';

    protected $hidden = ['id', 'parent_id', 'created_at', 'updated_at'];

    public function parent()
    {
        return $this->belongsTo(static::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(static::class, 'parent_id');
    }

    public function scopeGetNameByCode(Builder $builder, $code)
    {
        $data = $builder->whereCode($code)->first();

        return $data ? $data->name : "";
    }
}
